package com.appnet.android.truyencuoi;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends ArrayAdapter<ItemComic> {
	private ArrayList<ItemComic> mItemCommics;
	private Context mContext;
	private ItemOnClick mListener;
	private int[] resImgs = { R.drawable.ic_student, R.drawable.ic_dangian,
			R.drawable.ic_man, R.drawable.ic_world, R.drawable.ic_quan,
			R.drawable.ic_baby, R.drawable.ic_bman, R.drawable.ic_animal,
			R.drawable.ic_job };
	private int[] iconImgs = { R.drawable.emo1, R.drawable.emo2,
			R.drawable.emo3, R.drawable.emo4, R.drawable.emo5, R.drawable.emo6,
			R.drawable.emo7 };
	private int isMenu = 0;

	public ListViewAdapter(Context context, ArrayList<ItemComic> objects,
			ItemOnClick listener, int isMenu) {
		super(context, R.layout.item_title, objects);
		mItemCommics = objects;
		mContext = context;
		mListener = listener;
		this.isMenu = isMenu;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_title, parent, false);
		}
		ViewHolder holder = (ViewHolder) convertView.getTag();
		if (holder == null) {
			holder = new ViewHolder();
			holder.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
			holder.img = (ImageView) convertView.findViewById(R.id.ic_symbol);
			convertView.setTag(holder);
		}
		final ItemComic item = mItemCommics.get(position);
		holder.mTvName.setText(item.mValue);
		if (position < resImgs.length && isMenu == 1) {
			holder.img.setImageResource(resImgs[position]);
		}
		if(isMenu == 0){
			holder.img.setImageResource(iconImgs[position%6]);
		}
		// on click
		convertView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mListener.onClick(1, item.mKey);
			}
		});
		return convertView;
	}

	static class ViewHolder {
		public TextView mTvName;
		public ImageView img;
	}

}
